const id = layui.data('shop').id

axios.get(`http://localhost:9000/users/info?id=${id}`, { headers: { 'authorization': layui.data('shop').token } }).then(res => {
  if (res.data.code === 1) {
    layui.$('.username').val(res.data.user.username)
    layui.$('.age').val(res.data.user.age)
    layui.$('.gender').val(res.data.user.gender)
    layui.$('.nickname').val(res.data.user.nickname)
  }
})

layui.use(['form'], function () {
  var form = layui.form;
  var layer = layui.layer;

  // 表单验证
  form.verify({
    required: function (value, item) {
      if (!value) {
        return '该项为必填项';
      }
    }
  });

  form.on('submit(update)', function (data) {
    var username = data.field.username;
    var age = data.field.age;
    var gender = data.field.gender;
    var nickname = data.field.nickname;

    axios.post('http://localhost:9000/users/update', {
      id:id,
      age: age,
      gender: gender,
      nickname: nickname
    }, {
      headers: {
        'authorization': layui.data('shop').token
      }
    }).then(function (response) {
      if (response.data.code === 1) {
        layer.msg(response.data.message, { icon: 1 });
        layui.data('shop', {
          key: 'nickname',
          value: response.data.user.nickname
        })
      } else {
        layer.msg(response.data.message, { icon: 0 })
      }
    })

    return false;
  });
})